package com.elem.dao.impl;

import com.elem.bean.Business;
import com.elem.dao.BusinessDao;
import com.elem.util.DruidUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Collections;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    // 创建JdbcTemplate对象
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public List<Business> listBusinessByOrderTypeId(String orderTypeId) throws Exception {
        String sql = "select * from business where orderTypeId = ?";
        List<Business> businessList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Business>(Business.class), orderTypeId);
        return businessList;
    }

    @Override
    public Business getBusinessById(Integer businessId) throws Exception {
        try {
            String sql = "select * from business where businessId = ?";
            Business business = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Business.class), businessId);
            return business;
        } catch (DataAccessException e) {
            return null;
        }
    }
}
